package ie.dpsystems.errorlog;

import ie.dpsystems.dto.ErrorDetailsDTO;
import ie.dpsystems.dto.Parameters;
import ie.dpsystems.webservice.SimpleNETWebServiceConnector;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;

public class SyncService extends IntentService {
	public static final String ActionName = "ie.dpsystems.errorsycfinished";
 
    public SyncService() {
        super("SyncService");
    }
 
    @Override
    protected void onHandleIntent(Intent intent) {
 
    	Log.i("SyncService","Handling message");
    	
    	
    	Parameters p=new Parameters();		
    	p.LastErrorIdSynced = ErrorBasicDetails.GetLastSyncedErrorId(getApplicationContext());
		try {
			ErrorDetailsDTO[] serverResult = SimpleNETWebServiceConnector.ExecuteJson("http://92.60.118.192:3000/service1.asmx", "RetrieveErrors", ErrorDetailsDTO[].class, p);
			for ( ErrorDetailsDTO error : serverResult) {
				ErrorBasicDetails.InsertErrorBasicDetails(getApplicationContext(), error);
			}
			Intent i = new Intent(ActionName);
			sendBroadcast(i);
		} catch (Throwable e) {
			throw new RuntimeException(e);
		}
    }
}
